using System;
using System.Collections.Generic;
using System.Linq;
using Granite.ComponentModel;
using Granite.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Granite.Commons.Tests
{
    public class ItemEventAssert<T> : EventAssert<ItemEventArgs<T>>
    {

        public ItemEventAssert(object source, string eventName)
            : base(source)
        {
            var eventInfo = source.GetType().GetEvent(eventName);
            Action<object, ItemEventArgs<T>> handler = SourceEventFired;

            Delegate convertedHandler = ConvertDelegate(handler, eventInfo.EventHandlerType);
            eventInfo.AddEventHandler(source, convertedHandler);

        }

        public EventPair<ItemEventArgs<T>> Expect(T item)
        {
			var nextEvent = Expect();
            Assert.AreEqual(item, nextEvent.EventArgs.Item, "The wrong item was returned.");
            return nextEvent;
        }


        public static Delegate ConvertDelegate(Delegate originalDelegate, Type targetDelegateType)
        {
            return Delegate.CreateDelegate(
                targetDelegateType,
                originalDelegate.Target,
                originalDelegate.Method);
        }

    }
}
